/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PuntosJPanel.java
 *
 * Created on 11-may-2010, 20:19:23
 */

package tp_imag.gui;

import java.util.List;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import java.util.logging.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import tp_imag.gui.components.JImagePanel;
import tp_imag.model.ActualImage;
import tp_imag.model.SSSelement;
import tp_imag.service.ServiceFactory;
import tp_imag.service.ImageProcessor;
import tp_imag.service.NewHibernateUtil;
import tp_imag.service.ServiceFactory;
import tp_imag.service.metricSpaceIndex;
import tp_imag.service.impl.CoordenadaTransformada;
import tp_imag.service.impl.metricSpaceIndexImp;

/**
 *
 * @author Administrador
 */
public class PuntosJPanel extends javax.swing.JPanel {

    private ImageProcessor serv = ServiceFactory.getInstance().getImageProcessorService();
    private Vector<CoordenadaTransformada> vt;

    /** Creates new form PuntosJPanel */
    public PuntosJPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanelOriginal = new javax.swing.JPanel();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Imagen Original"));
        jPanel2.setName("jPanel2"); // NOI18N

        jPanelOriginal.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanelOriginal.setName("jPanelOriginal"); // NOI18N

        javax.swing.GroupLayout jPanelOriginalLayout = new javax.swing.GroupLayout(jPanelOriginal);
        jPanelOriginal.setLayout(jPanelOriginalLayout);
        jPanelOriginalLayout.setHorizontalGroup(
            jPanelOriginalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 392, Short.MAX_VALUE)
        );
        jPanelOriginalLayout.setVerticalGroup(
            jPanelOriginalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 139, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelOriginal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelOriginal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.getAccessibleContext().setAccessibleName("Imagen de Puntos");
    }// </editor-fold>//GEN-END:initComponents

    private BufferedImage loadImg;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelOriginal;
    // End of variables declaration//GEN-END:variables

     public void loadScreen() {
        loadImage();
        showOriginalImg();
    }

    public Vector<CoordenadaTransformada> getCoordenadaTransformada(){
        return this.vt;
    }

    private void loadImage() {
        try {
            //ActualImage.getInstance().setBinarized(serv.binarizeImage(ActualImage.getInstance().getOriginal()));
            loadImg = ActualImage.getInstance().getBinarized();
            // Lo siguiente se comenta ya qeu ese seteo se esta haciendo dentro del vectorizar
            // solo se ejecuta el servicio para vectorizar
            vt=serv.vectorizeImage(loadImg, "vectorizada");
            //ActualImage.getInstance().setVectorized(serv.vectorizeImage(loadImg));
            loadImg = ActualImage.getInstance().getVectorized();
            
            /* Busco en el indice la lista de contornos que se parecen 
             * 
             */
            //String fileName="C:\\Fotos\\indice.dat";
            //ArrayList<double[]> soluciones=new ArrayList<double[]>();
            ArrayList<Integer> soluciones=new ArrayList<Integer>();
            //metricSpaceIndex index=metricSpaceIndexImp.load(fileName);
            double[] vm=ServiceFactory.getInstance().getMetricSpaceIndexService().CalcularModulo(vt);            
            //ServiceFactory.getInstance().getMetricSpaceIndexService().rangeSearch(vm, (byte)5, soluciones);
            ServiceFactory.getInstance().getMetricSpaceIndexService().rangeSearch2(vm, (byte)5, soluciones);
            
            //System.out.println("Cantidad de soluciones: " + soluciones.size());
            String parametros="";
            for (int i = 0; i < soluciones.size(); i++) {
                System.out.println("solucion: " + ((int)soluciones.get(i)));
                parametros+=","+String.valueOf(soluciones.get(i));
            }
            parametros = parametros.replaceFirst(",", "");
            //System.out.println("solucion: " + parametros);
                try {
                    Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
                    Transaction tx = session.beginTransaction();

                    List elementosGuardados = session.createQuery("select e from SSSelement as e where id in (" + parametros + ") order by id asc").list();
                    for (Iterator iter = elementosGuardados.iterator(); iter.hasNext();) {
                        SSSelement sssElement = (SSSelement) iter.next();
                        System.out.println(sssElement.getPathOriginal());
                    }
                    tx.commit();
                } catch (RuntimeException e) {
                    // throw again the first exception
                    throw e;
                }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void showOriginalImg() {

        try {
            //BufferedImage loadImg = ImageIO.read(new File("Perfil3.jpg"));
            jPanelOriginal.setLocation(1, 1);
            int h = loadImg.getHeight();
            int w = loadImg.getWidth();

            jPanelOriginal.setSize(w, h);
            JImagePanel panel = new JImagePanel(loadImg, 0, 0, jPanelOriginal.getSize());

            jPanelOriginal.setLayout(new BorderLayout());
            panel.setVisible(true);
            jPanelOriginal.add(panel);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
